package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.feign.client.AddressClient;
import com.hmall.feign.client.ItemClient;
import com.hmall.feign.dto.Address;
import com.hmall.feign.dto.Item;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.IOrderDetailService;
import com.hmall.order.service.IOrderLogisticsService;
import com.hmall.order.service.IOrderService;
import com.hmall.order.utils.IdWorker;
import com.hmall.order.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

@Slf4j
@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Resource
    private IdWorker idWorker;

    @Resource
    private ItemClient itemClient;

    @Resource
    private AddressClient addressClient;

    @Resource
    private IOrderDetailService orderDetailService;

    @Resource
    private IOrderLogisticsService orderLogisticsService;


    /**
     * 提交订单
     *
     * @param params
     * @return
     */
    @Override
    @Transactional
    public Long submitOrder(RequestParams params) {
        // 查询商品信息
        Item item = itemClient.queryItemById(params.getItemId());
        // 计算商品总价
        long totalFee = item.getPrice() * params.getNum();

        Order order = new Order();
        // 设置订单ID
        order.setId(idWorker.nextId());
        // 设置订单初始状态为未支付
        order.setStatus(1);
        // 设置订单总金额
        order.setTotalFee(totalFee);
        // 设置支付方式
        order.setPaymentType(params.getPaymentType());
        // 设置用户ID
        order.setUserId(UserHolder.getUser());
        // 订单创建时间
        order.setCreateTime(new Date());
        // 保存订单信息
        this.save(order);

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId());
        orderDetail.setItemId(params.getItemId());
        orderDetail.setNum(params.getNum());
        orderDetail.setTitle(item.getName());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setImage(item.getImage());
        orderDetail.setCreateTime(new Date());
        orderDetailService.save(orderDetail);

        Address address = addressClient.findAddressById(params.getAddressId());
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setOrderId(order.getId());
        orderLogistics.setLogisticsNumber("A123456789");
        orderLogistics.setLogisticsCompany("菜鸟物流");
        orderLogistics.setContact(address.getContact());
        orderLogistics.setPhone(address.getMobile());
        orderLogistics.setProvince(address.getProvince());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setStreet(address.getStreet());
        orderLogistics.setCreateTime(new Date());
        orderLogisticsService.save(orderLogistics);

        // 扣除库存
        itemClient.updateStock(params.getItemId(), params.getNum());
        return order.getId();
    }
}
